from tkinter import*
from tkinter import colorchooser
from tkinter.colorchooser import *
root=Tk()
root.geometry("800x600")
frame1=Frame(root)
cav=Canvas(frame1,width=800,height=500,bg="white")
startx=0
starty=0
lastline=None
color="red"
fcolor="red"
type="画线"
def mouseClick(event):
    global startx
    global starty
    startx=event.x
    starty=event.y
    print(event.x,event.y)
def mouseMove(event):
    global lastline
    endx=event.x
    endy=event.y
    cav.delete(lastline)
    if(type=="画线"):
        lastline=cav.create_line(startx,starty,endx,endy,fill=fcolor)
    elif(type=="画圆"):
        lastline=cav.create_oval(startx,starty,endx,endy,fill=fcolor)
    else:
        lastline=cav.create_rectangle(startx,starty,endx,endy,fill=fcolor)
def mouseRelease(event):
    global lastline
    lastline=None
cav.bind("<Button-1>",mouseClick)
cav.bind("<B1-Motion>",mouseMove)
cav.bind("<ButtonRelease-1>",mouseRelease)
cav.pack()
def btClick(event):
    global type
    type=event.widget["text"]
    print(type)
def btColorClick(event):
    global fcolor
    s1=askcolor(color="red",title="选择颜色")
    fcolor=s1[1]
btLine=Button(frame1,text="画线",font="宋体,8")
btLine.bind("<Button-1>",btClick)
btLine.pack()
btOval=Button(frame1,text="画圆",font="宋体,8")
btOval.bind("<Button-1>",btClick)
btOval.pack()
btrectangle=Button(frame1,text="画长方形",font="宋体,8")
btrectangle.bind("<Button-1>",btClick)
btrectangle.pack()
btColor=Button(frame1,text="画笔颜色",font="宋体,8")
btColor.bind("<Button-1>",btColorClick)
btColor.pack()
#创建菜单
frame1.pack()
root.mainloop()